package jm.nj.demo08mian.demo04;



/*
*
*
* */
public class Person {
    private String name;
    private int age;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    /*
        * 重写Object类的hashCode方法
        * 可以模仿String类:"姓名".hashCode+年龄
        *
        * set.add(new Person("a"),12)哈希值:"a" +12 = 109
        * set.add(new Person("b"),11)哈希值:"b" +11 = 109
        * 出现以上的情况,元素不一致,但是哈希值相同,降低不同元素相同哈希值的概率(提高程序的效率)
         *  name.hashCode()+age*2;
         * set.add(new Person("a",12)); 哈希值:"a" +24 = 121
         * set.add(new Person("b",11)); 哈希值:"b" +22 = 120
         * 	 name.hashCode()+age*31;
        *
    ;    * */
    @Override
    public int hashCode(){
        return name.hashCode()+age*31;
    }

    /*
    * 重写Object类中的equals方法
    * 两个人的名字和年龄相同返回true,否则返回false
    * */
    @Override
    public boolean equals (Object obj){
        if (obj==this){
            return true;
        }
        if (obj == null){
            return false;
        }
        //判断是否为person
        if(obj instanceof Person){
            Person p = (Person)obj;

            return this.name.equals(p.name) && this.age == p.age;
        }

        return false;

    }



    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Person() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
